<?php
/**
 * Conditions entity
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    WeatherApi
 */
namespace Celebrio\WeatherApi\WorldWeatherOnline;

use Nette\Object;
use Nette\Environment;
use \MemberAccessException;

abstract class WeatherConditions extends Object {
    private $weatherCode;
    private $weatherIconUrl;
    private $weatherDesc;
    private $windspeedMiles;
    private $windspeedKmph;
    private $winddirDegree;
    private $winddir16Point;
    private $winddirNat;
    private $precipMM;
    
    public function getWeatherCode() {
        return $this->weatherCode;
    }

    public function setWeatherCode($weatherCode) {
        $this->weatherCode = $weatherCode;
    }

    public function getWeatherIconUrl() {
        return $this->weatherIconUrl;
    }

    public function setWeatherIconUrl($weatherIconUrl) {
        $icon = $this->codeToLink($this->getWeatherCode());
        if($icon !== FALSE) {
            $this->weatherIconUrl = $icon;
        } else {
            $this->weatherIconUrl = $weatherIconUrl;
        }
        $this->codeToLink($this->getWeatherCode());
    }

    public function getWeatherDesc() {
        return $this->weatherDesc;
    }

    public function setWeatherDesc($weatherDesc) {
        $this->weatherDesc = $weatherDesc;
        //TODO: Az to bude hotovy, tak tohle odkomentovat a nahradit
        //$this->codeToDescription($this->weatherCode);
    }

    public function getWindspeedMiles() {
        return $this->windspeedMiles;
    }

    public function setWindspeedMiles($windspeedMiles) {
        $this->windspeedMiles = $windspeedMiles;
    }

    public function getWindspeedKmph() {
        return $this->windspeedKmph;
    }

    public function setWindspeedKmph($windspeedKmph) {
        $this->windspeedKmph = $windspeedKmph;
    }

    public function getWinddirDegree() {
        return $this->winddirDegree;
    }

    public function setWinddirDegree($winddirDegree) {
        $this->winddirDegree = $winddirDegree;
    }

    public function getWinddir16Point() {
        return $this->winddir16Point;
    }

    public function setWinddir16Point($winddir16Point) {
        $this->winddir16Point = $winddir16Point;
        $this->setWinddirNat($winddir16Point);
    }
    
    
    public function getWinddirNat() {
        return $this->winddirNat;
    }

    public function setWinddirNat($winddirNat) {
        $this->winddirNat = $this->convertWindDirection($winddirNat);
    }

        
    public function getPrecipMM() {
        return $this->precipMM;
    }

    public function setPrecipMM($precipMM) {
        $this->precipMM = $precipMM;
    }

    /*public function setAttribute($name, $value) {
        if(empty($value) || empty($name)) {
            return false;
        }
        $name = "set" . ucfirst($name);
        try {
            $this->$name($value);
        } catch (MemberAccessException $e) {
            //TODO: log an error here
        }
    }
    
    private function codeToDescription($code) {
        
        switch ($code) {
            case 113:
                $this->weatherDesc(_("Clear / Sunny"));
                break;
            
            case 116:
                $this->weatherDesc(_("Partly cloudy"));
                break;
            
            case 119:
                $this->weatherDesc(_("Cloudy"));
                $this->eatherIconUrl($code);
                break;
            
            case 122:
                $this->weatherDesc(_("Overcast"));
                break;
            
            case 143:
                $this->weatherDesc(_("Mist"));
                break;
            
            case 176:
                $this->weatherDesc(_("Patchy rain nearby"));
                break;
            
            case 179:
                $this->weatherDesc(_("Patchy snow nearby"));
                break;
            
            case 182:
                $this->weatherDesc(_("Patchy sleet nearby"));
                break;
            
            case 185:
                $this->weatherDesc(_("Patchy freezing drizzle nearby"));
                break;
            
            case 200:
                $this->weatherDesc(_("Thundery outbreaks in nearby"));
                break;
            
            case 227:
                $this->weatherDesc(_("Blowing snow"));
                break;
            
            case 230:
                $this->weatherDesc(_("Blizzard")); //entertainment and blizzard north presents ...
                break;
            
            case 248:
                $this->weatherDesc(_("Fog"));
                break;
            
            case 260:
                $this->weatherDesc(_("Freezing fog")); //it sounds more like some pokemon attack 
                break;
            
            case 263:
                $this->weatherDesc(_("Patchy light drizzle"));
                break;
            
            case 266:
                $this->weatherDesc(_("Light drizzle"));
                break;
            
            case 281:
                $this->weatherDesc(_("Freezing drizzle"));
                break;
            
            case 284:
                $this->weatherDesc(_("Heavy freezing drizzle"));
                break;
            
            case 293:
                $this->weatherDesc(_("Patchy light rain"));
                break;
            
            case 296:
                $this->weatherDesc(_("Light rain"));
                break;
            
            case 299:
                $this->weatherDesc(_("Moderate rain at times"));
                break;
            
            case 302:
                $this->weatherDesc(_("Moderate rain"));
                break;
            
            case 305:
                $this->weatherDesc(_("Heavy rain at times"));
                break;
            
            case 308:
                $this->weatherDesc(_("Heavy rain"));
                break;
            
            case 311:
                $this->weatherDesc(_("Light freezing rain"));
                break;
            
            case 314:
                $this->weatherDesc(_("Moderate or heavy freezing rain"));
                break;
            
            case 317:
                $this->weatherDesc(_("Light sleet"));
                break;

            case 320:
                $this->weatherDesc(_("Moderate or heavy sleet"));
                break;
            
            case 323:
                $this->weatherDesc(_("Patchy light snow"));
                break;
            
            case 326:
                $this->weatherDesc(_("Light snow"));
                break;
            
            case 329:
                $this->weatherDesc(_("Patchy moderate snow"));
                break;
            
            case 332:
                $this->weatherDesc(_("Moderate snow"));
                break;
            
            case 335:
                $this->weatherDesc(_("Patchy heavy snow"));
                break;
            
            case 338:
                $this->weatherDesc(_("Heavy snow"));
                break;
            
            case 350:
                $this->weatherDesc(_("Ice pellets"));
                break;
            
            case 353:
                $this->weatherDesc(_("Light rain shower"));
                break;
            
            case 356:
                $this->weatherDesc(_("Moderate or heavy rain shower"));
                break;
            
            case 359:
                $this->weatherDesc(_("Torrential rain shower"));
                break;
            
            case 362:
                $this->weatherDesc(_("Light sleet showers"));
                break;
            
            case 365:
                $this->weatherDesc(_("Moderate or heavy sleet showers"));
                break;
            
            case 368:
                $this->weatherDesc(_("Light snow showers"));
                break;
            
            case 371:
                $this->weatherDesc(_("Moderate or heavy snow showers"));
                break;
            
            case 374:
                $this->weatherDesc(_("Light showers of snow pellets"));
                break;
            
            case 377:
                $this->weatherDesc(_("Moderate or heavy showers of snow pellets"));
                break;
            
            case 386:
                $this->weatherDesc(_("Patchy light rain in area with thunder"));
                break;
            
            case 389:
                $this->weatherDesc(_("Moderate or heavy rain in area with thunder"));
                break;
            
            case 392:
                $this->weatherDesc(_("Patchy light snow in area with thunder"));
                break;
            
            case 395:
                $this->weatherDesc(_("Moderate or heavy snow with area with thunder"));
                break;
            
            default:
                $this->weatherDesc("Not available");
                break;
        }
    }*/
    //TODO: make this complete and refactor
    private function codeToLink($code) {
        $basePath = Environment::getHttpRequest()->getUri()->getBasePath();
        $path= $basePath . "systemApps/celebrioweather/images/icons/";
        $linksToImages = array(
            113 => "sunny/",
            116 => "sunny_intervals/",
            119 => "white_clouds/",
            122 => "white_clouds/",
            143 => "mist/",
            176 => "light_rain_showers/",
            179 => "sleet_showers/",
            182 => "cloudy_with_sleet/",
            185 => "cloudy_with_sleet/",
            200 => "thundery_showers/",
            227 => "cloudy_with_light_snow/",
            230 => "cloudy_with_heavy_snow/",
            248 => "mist/",
            260 => "mist/",
            263 => "light_rain_showers/",
            266 => "cloudy_with_light_rain/",
            281 => "cloudy_with_sleet/",
            284 => "cloudy_with_sleet/",
            293 => "cloudy_with_light_rain/",
            296 => "cloudy_with_light_rain/",
            299 => "heavy_rain_showers/",
            302 => "cloudy_with_heavy_rain/",
            305 => "heavy_rain_showers/",
            308 => "cloudy_with_heavy_rain/",
            311 => "cloudy_with_sleet/",
            314 => "cloudy_with_sleet/",
            317 => "cloudy_with_sleet/",
            320 => "cloudy_with_light_snow/",
            323 => "light_snow_showers/",
            326 => "light_snow_showers/",
            329 => "cloudy_with_heavy_snow/",
            332 => "cloudy_with_heavy_snow/",
            335 => "heavy_snow_showers/",
            338 => "cloudy_with_heavy_snow/",
            350 => "cloudy_with_sleet/",
            353 => "light_rain_showers/",
            356 => "heavy_rain_showers/",
            359 => "cloudy_with_heavy_rain/",
            362 => "sleet_showers/",
            365 => "sleet_showers/",
            368 => "light_snow_showers/",
            371 => "heavy_snow_showers/",
            374 => "sleet_showers/",
            377 => "cloudy_with_sleet/",
            386 => "thundery_showers/",
            389 => "thunderstorm/",
            392 => "thundery_showers/",
            395 => "heavy_snow_showers/"
        );
        if(array_key_exists($code, $linksToImages)) {
            return $path . $linksToImages[$code] . "160dpi/icon.png";
        } else {
            return false;
        }
    }
    
    private function convertWindDirection($wind_dir) {
        switch($wind_dir) {
            case "N":
                return _("North");
                break;
            
            case "E":
                return _("East");
                break;
            
            case "S":
                return _("South");
                break;
            
            case "W":
                return _("West");
                break;
            
            case "NE":
                return _("North-east");
                break;
            
            case "NW":
                return _("North-west");
                break;
            
            case "SE":
                return _("South-east");
                break;
            
            case "SW":
                return _("South-west");
                break;
            
            case "NNE":
                return _("North-north-east");
                break;
            
            case "NNW":
                return _("North-north-west");
                break;
            
            case "SSE":
                return _("South-south-east");
                break;
            
            case "SSW":
                return _("South-south-west");
                break;
            
            case "ENE":
                return _("East-north-east");
                break;
            
            case "WNW":
                return _("West-north-west");
                break;
            
            case "ESE":
                return _("East-south-east");
                break;
            
            case "WSW":
                return _("West-south-east");
                break;
            
            default: 
                return _("North");
                break;
        }
    }
}